# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=dropbear
pkgver=2022.83
pkgrel=4
pkgdesc="small SSH 2 client/server designed for small memory environments"
url="https://matt.ucc.asn.au/dropbear/dropbear.html"
arch="all"
license="MIT"
options="!check"
makedepends="zlib-dev linux-headers utmps-dev utmps-static"
subpackages="$pkgname-doc
	$pkgname-openrc
	$pkgname-convert:_convert
	$pkgname-dbclient:_dbclient
	$pkgname-ssh:_ssh:noarch
	$pkgname-scp:_scp"
source="https://matt.ucc.asn.au/dropbear/releases/dropbear-$pkgver.tar.bz2
	dropbear.initd
	dropbear.confd
	dropbear-options_sftp-server_path.patch
	dropbear-fix-utmp.patch
	Implement-Strict-KEX-mode.patch
	"

# secfixes:
#   2022.83-r4:
#     - CVE-2023-48795
#   2020.79-r0:
#     - CVE-2018-20685
#   2018.76-r2:
#     - CVE-2018-15599

_progs="dropbear dropbearkey dbclient dropbearconvert scp"

build() {
	CFLAGS="$CFLAGS $(pkg-config --cflags --static utmps)" \
	LIBS="$LDLIBS $(pkg-config --libs --static utmps)" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--disable-utmp \
		--disable-wtmp \
		--disable-pututline \
		--disable-lastlog
	make PROGRAMS="$_progs"
}

package() {
	make PROGRAMS="$_progs" DESTDIR="$pkgdir" install

	install -dm755 "$pkgdir"/etc/dropbear
	install -D -m755 "$srcdir"/dropbear.initd \
		"$pkgdir"/etc/init.d/dropbear
	install -D -m644 "$srcdir"/dropbear.confd \
		"$pkgdir"/etc/conf.d/dropbear
}

_convert() {
	pkgdesc="$pkgname dropbearconvert command"
	depends=""
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/dropbearconvert "$subpkgdir"/usr/bin/
}

_dbclient() {
	pkgdesc="$pkgname dbclient command"
	depends=""
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/dbclient "$subpkgdir"/usr/bin/
}

_scp() {
	pkgdesc="$pkgname scp support"
	depends="!openssh-client"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/scp "$subpkgdir"/usr/bin/
}

_ssh() {
	pkgdesc="$pkgname ssh command (compatibility symlink for dbclient)"
	depends="dropbear-dbclient !openssh-client"
	mkdir -p "$subpkgdir"/usr/bin
	ln -s /usr/bin/dbclient "$subpkgdir"/usr/bin/ssh
}

sha512sums="
c63afa615d64b0c8c5e739c758eb8ae277ecc36a4223b766bf562702de69910904cbc3ea98d22989df478ae419e1f81057fe1ee09616c80cb859f58f44175422  dropbear-2022.83.tar.bz2
78bd4142f9ad05b40520de6dbe71cd64fb2b3bf2d346f7710aa2ab2313162ea17be13fa27e8f8c645307c9d32f640e05ec1d92d303936e60feb8e95cc4369f5c  dropbear.initd
83f2c1eaf7687917a4b2bae7d599d4378c4bd64f9126ba42fc5d235f2b3c9a474d1b3168d70ed64bb4101cc251d30bc9ae20604da9b5d819fcd635ee4d0ebb0f  dropbear.confd
df3fa2c64665b669257ec511afa2e04107eb6b0a2a4238ad9b33db3d322b90854b00506e14a09b648fe2d716dc6c140212cc1739f7cd03088f9b7b9781aaa499  dropbear-options_sftp-server_path.patch
e143b8b2083c96fdf425fbe179aa43892e44b95734686a57ae5d5612778f1bd7e78a725d8413c1701d37dcf1b24986b1faa7365eb039789e2204320272fed7a3  dropbear-fix-utmp.patch
d747d4b083255634962ee0d42454093564930555b5f8bff1fff64e2e5e399c47d3b53ddb54356bb3ad2cc7d8b7f017193dcd889ff49021b3d43d3f38b5b0d616  Implement-Strict-KEX-mode.patch
"
